<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>选择所有对角单元格</title>
	</head>
	<body>
		<table border id="myTable">
			<tbody>
				<tr>
					<td>1:1</td>
					<td>2:1</td>
					<td>3:1</td>
					<td>4:1</td>
					<td>5:1</td>
				</tr>
				<tr>
					<td>1:2</td>
					<td>2:2</td>
					<td>3:2</td>
					<td>4:2</td>
					<td>5:2</td>
				</tr>
				<tr>
					<td>1:3</td>
					<td>2:3</td>
					<td>3:3</td>
					<td>4:3</td>
					<td>5:3</td>
				</tr>
				<tr>
					<td>1:4</td>
					<td>2:4</td>
					<td>3:4</td>
					<td>4:4</td>
					<td>5:4</td>
				</tr>
				<tr>
					<td>1:5</td>
					<td>2:5</td>
					<td>3:5</td>
					<td>4:5</td>
					<td>5:5</td>
				</tr>
			</tbody>
		</table>

		<script>
			// 获取表格所有的行
			let trs = myTable.rows

			// 遍历行，给每一行的单元格根据当前行索引设置背景色
			for (let i = 0; i < trs.length; i++) {
				trs[i].cells[i].style.backgroundColor = 'red'
			}
		</script>
	</body>
</html>
